#!/usr/bin/env node

const http = require('https'),
      //log  = console.log,
      url = require('url'),
      cheerio = require('cheerio'),
      addr = 'https://ke.sifou.com/free';

let opt = url.parse(addr);
opt.headers = {
  'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'
};

http.get(opt,(res)=>{
  let data = '';

  res.on('data',(chunk)=> data += chunk);

  res.on('end',()=>{
    let $ = cheerio.load(data);
    let courses =  $('body').find('h5.card-title>a');

    var message = [];
    
    for(let i = 0 ; i<courses.length; i++){
      
      let str = $(courses[i]).attr('href');
      let id = str.slice(8);

      var msg = {
        'Name':$(courses[i]).text(),
        'Id':id
      };
      message.push(msg);
    }

     console.log(message);
  });
});
